﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using B.S.BasicData.Api.Services;
using B.S.BasicData.EroorCode;

namespace B.S.BasicData.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class QiniuUploadController : ControllerBase
    {
        private readonly QiniuUploadService _service;

        public QiniuUploadController(QiniuUploadService service)
        {
            _service = service;
        }

        [HttpPost]
        public async Task<IActionResult> UploadImage(IFormFile file)
        {
            if (file == null || file.Length == 0)
                return BadRequest("没有上传图片");

            var url = await _service.UploadImageAsync(file);
            return Ok(new { url });
        }

        [HttpPost]
        public async Task<IActionResult> UploadImages(List<IFormFile> files)
        {
            if (files == null || files.Count == 0)
                return BadRequest("没有上传图片");

            var urls = await _service.UploadImagesAsync(files);
            return Ok(new { urls });
        }

        /// <summary>
        /// 删除七牛云上的文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>删除结果</returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteFile([FromQuery] string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                return BadRequest(ApiResult<bool>.Fail("文件名不能为空", ApiResultCode.Error));

            var success = await _service.DeleteFileAsync(fileName);
            if (success)
                return Ok(ApiResult<bool>.Success(true, "删除成功"));
            else
                return StatusCode(500, ApiResult<bool>.Fail("删除失败", ApiResultCode.Error));
        }
    }
}